TOOL.Category		= "(WarMelons)"
TOOL.Name			= "#Order Cores"
TOOL.Command		= nil
TOOL.ConfigName		= ""

TOOL.ClientConVar[ "teamnumber" ] = "1"
TOOL.ClientConVar[ "gravity" ] = "1"
TOOL.ClientConVar[ "moveforce" ] = "1"
TOOL.ClientConVar[ "welded" ] = "1"
TOOL.ClientConVar[ "damping" ] = "1.2"
TOOL.ClientConVar[ "marine" ] = "0"

if (CLIENT) then
	language.Add( "Tool_ordermelons_name", "Orderable Melon Cores" )
	language.Add( "Tool_ordermelons_desc", "Unarmed warmelons to pull your contraptions with" )
	language.Add( "Tool_ordermelons_0", "Left-click to spawn a warmelon. Right-click to display cost." )
	language.Add( "Undone_ordermelons", "Undone Warmelon" )
end

function TOOL:LeftClick (trace)
if ( SERVER ) then
	local entz = ents.FindByClass("lap_spawnpoint");
	local ply = self:GetOwner()
	local disfin = 0
	local Pos = trace.HitPos
	local Ang = trace.HitNormal
	local buildradius = server_settings.Int( "WM_BuildRadius", 750 )
	local cteam = 0
	if ply:GetNetworkedInt("melonteam") ~= 0 then
	cteam = ply:GetNetworkedInt("melonteam")
	else
	cteam = self:GetClientNumber("teamnumber")
	end
	if buildradius ~= 0 then
	for k, v in pairs(entz) do
	local dist = v:GetPos():Distance(Pos);
		if (v.Team == cteam && dist <= buildradius) then
		disfin = 1
		end
	end
	else
	disfin = 1
	end
	local nrgsup = ply:GetNetworkedInt( "nrg" )
	local toolcost = server_settings.Int( "WM_Toolcost", 1 )
	local cost = (2000+self:GetClientNumber("moveforce")*4+self:GetClientNumber("damping")*100-self:GetClientNumber("gravity")*1000+self:GetClientNumber("marine")*500)*toolcost
	local fcost = self:GetClientNumber("moveforce")*4
	if (trace.Hit && trace.Entity:IsValid() && trace.Entity:GetClass() == "johnny_ordermelon" && nrgsup >= fcost) then
		--We've shot an order core. We should update the melon's force to the new move force setting in the context panel.
		ply:SetNetworkedInt( "nrg" , nrgsup - fcost)		
		trace.Entity:GetTable().MovingForce = self:GetClientNumber("moveforce");
		return true;
	else 
		if (trace.Hit && !trace.HitNoDraw && nrgsup >= cost) then
		if ( disfin == 1 ) then
		ply:SetNetworkedInt( "nrg" , nrgsup - cost)
		melon = ents.Create ("johnny_ordermelon");
		if (self:GetClientNumber("welded") == 0) then
		melon:SetPos (trace.HitPos + trace.HitNormal * 12);
		else
		melon:SetPos (trace.HitPos + trace.HitNormal);
		end
		melon:SetAngles (trace.HitNormal);
		melon:GetTable().Team = cteam;
		melon:GetTable().MovingForce = self:GetClientNumber("moveforce");
		melon:GetTable().Damping = self:GetClientNumber("damping");
		melon:GetTable().Grav = self:GetClientNumber("gravity") == 1;
		melon:GetTable().Marine = self:GetClientNumber("marine") == 1;
		melon:Spawn ();
		melon:Activate ();
		melon:GetPhysicsObject():SetMass(100);
		melon:GetPhysicsObject():EnableGravity(self:GetClientNumber("gravity") == 1);
		if (self:GetClientNumber("welded") == 1) then upright = constraint.Weld (melon, trace.Entity, 0, trace.PhysicsBone, 0, true) end
		trace.Entity:DeleteOnRemove(melon);
		undo.Create("ordermelons");
		undo.AddEntity (melon);
		if (self:GetClientNumber("welded") == 1) then undo.AddEntity (upright) end
		undo.SetPlayer (self:GetOwner());
		undo.Finish();
		return true;
		else
		ply:PrintMessage(HUD_PRINTTALK, "Too far from spawn point")
		end
		else 
		local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
		ply:PrintMessage(HUD_PRINTTALK, message)
		return false end
		end
	end
end

function TOOL:RightClick (trace)
if (SERVER) then
local ply = self:GetOwner()
local toolcost = server_settings.Int( "WM_Toolcost", 1 )
local cost = (2000+self:GetClientNumber("moveforce")*5+self:GetClientNumber("damping")*100-self:GetClientNumber("gravity")*1000+self:GetClientNumber("marine")*500)*toolcost

	local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
	ply:PrintMessage(HUD_PRINTTALK, message)
end
	return true
end

function TOOL.BuildCPanel (CPanel)
	CPanel:AddControl ("Header", { Text="#Tool_ordermelons_name", Description="#Tool_ordermelons_desc" })
	CPanel:AddControl ("Slider", {
		Label = "Team number",
		Command = "ordermelons_teamnumber",
		Type = "Integer",
		Min = "1",
		Max = "6"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Gravity toggle",
		Description = "Uncheck box to make melons have no gravity",
		Command = "ordermelons_gravity"
	} )
	CPanel:AddControl ("Slider", {
		Label = "Movement Force",
		Command = "ordermelons_moveforce",
		Type = "Float",
		Min = "1",
		Max = "50000"
	} )
	CPanel:AddControl ("Slider", {
		Label = "Braking Power",
		Command = "ordermelons_damping",
		Type = "Float",
		Min = "0",
		Max = "20"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Weld",
		Description = "Uncheck box and melons will not be attached to what you shoot at",
		Command = "ordermelons_welded"
	} )
	CPanel:AddControl( "Checkbox", {
		Label = "Marine",
		Description = "Check box and melons will be able to traverse water unharmed",
		Command = "ordermelons_marine"
	} )
end
